<?php
class App_Line extends Data_Item
{
    public function __construct($lineId = NULL)
   {
    //$this->_commTableName = 'mod_ziying';
    $this->_commPrimaryKey = 'id';
    $this->_lineId = (int) $lineId;
    $this->_nid     = $this->_lineId;
   }
   //合并track和activity
    public function get_track_and_act($guiderUid = NULL, $offset = 0, $limit = 10, $sortkey = NULL,$sorttype='desc')
   {
	$query1 = DB::select('uid','id','city','name','thumb','summary','priceTotal','changed','type','zan')
		->from('mod_track')
		->where('status', '=', 'VALID');
		
	if ($guiderUid > 0)
	{
		$query1=$query1->where('uid', '=', $guiderUid);
	}
	if(!empty($sortkey))
	{
		$query1=$query1->order_by($sortkey,$sorttype);
	}
	
	if ($limit > 0)
	{
		$query1=$query1->offset($offset)->limit($limit);
	}
		
	$query = DB::select('uid','id','city','name','thumb','summary','priceTotal','changed','type','zan')
		->union($query1)
		->from('mod_activity')
		->where('status', '=', 'VALID');
	if ($guiderUid > 0)
	{
		$query=$query->where('uid', '=', $guiderUid);
	}
		 		 
	$query  = $query->execute();
		 
	$items = array();
	foreach ($query as $rs)
	{
		$guider = new Guider($rs['uid']);
		$rs['guider'] = $guider->getUserInfo();
		$rs['type'] = strtolower($rs['type']);
		$items[] = $rs;
	}
		 
	return $items;
	
    }
    //合并track、activity和ziying
    public function get_track_act_zy($guiderUid = NULL, $offset = 0, $limit = 10, $sortkey = NULL,$sorttype='desc')
   {
   	$count1 = DB::select('id')
   	->from('mod_track')
   	->where('status', '=', 'VALID');
   	if ($guiderUid > 0)
   	{ $count1=$count1->where('uid', '=', $guiderUid);}
   	
   	$count2 = DB::select('id')
   	->union($count1)
   	->from('mod_activity')
        ->where('status', '=', 'VALID');
         if ($guiderUid > 0)
        { $count2=$count2->where('uid', '=', $guiderUid);}
   	
   	$count3 = DB::select('id')
   	->union($count2)
   	->from('mod_ziying')
        ->where('status', '=', 'VALID');
         if ($guiderUid > 0)
        { $count3=$count3->where('uid', '=', $guiderUid);}
   	//-------------------------------------------------------
        $query1 = DB::select('uid','id','city','name','thumb','summary','priceTotal','changed','type','zan','serviceMaxPersons','orders','score')
   	->from('mod_track')
   	->where('status', '=', 'VALID');
   	if ($guiderUid > 0)
   	{ $query1=$query1->where('uid', '=', $guiderUid);}
   	if(!empty($sortkey))
   	{ $query1=$query1->order_by($sortkey,$sorttype);}
   	
   	if ($limit > 0)
   	{ $query1=$query1->offset($offset)->limit($limit);}
   	
   	$query2 = DB::select('uid','id','city','name','thumb','summary','priceTotal','changed','type','zan','serviceMaxPersons','orders','score')
   	->union($query1)
   	->from('mod_activity')
        ->where('status', '=', 'VALID');
         if ($guiderUid > 0)
        { $query2=$query2->where('uid', '=', $guiderUid);}
   	
   	$query3 = DB::select('uid','id','city','name','thumb','summary','priceTotal','changed','type','zan','serviceMaxPersons','orders','score')
   	->union($query2)
   	->from('mod_ziying')
        ->where('status', '=', 'VALID');
         if ($guiderUid > 0)
        { $query3=$query3->where('uid', '=', $guiderUid);}
   	
   	$query  = $query3->execute()->as_array();
   	$counts =$count3->execute()->count();
   	
   	$items = array();
   	foreach ($query as $rs)
   	{
   		$guider = new Guider($rs['uid']);
   		$rs['guider'] = $guider->getUserInfo();
   		$rs['type'] = strtolower($rs['type']);
   		if($rs['type']=='activity')
   		{
   			$rs['hours']=DB::select('hours')->from('mod_activity')->where('id','=',$rs['id'])
   			             ->limit(1)
   			             ->execute()->get('hours');
   		}
   		elseif($rs['type']=='track')
   		{
   			$track_info=DB::select('trafficSupportBus','trafficSupportCar','days')
   			            ->from('mod_track')
   			            ->where('id','=',$rs['id'])
   			            ->limit(1)
   			            ->execute()->as_array();
   			$rs['trafficSupportBus']=$track_info[0]['trafficSupportBus'];
   			$rs['trafficSupportCar']=$track_info[0]['trafficSupportCar'];
   			$rs['days']=$track_info[0]['days'];
   		}
   		else
   		{
   			$rs['days']=DB::select('days')->from('mod_ziying')->where('id','=',$rs['id'])
   			             ->limit(1)
   			             ->execute()->get('days');
   		}
   		$items[] = $rs;
   	}
   	
   	return array($counts,$items);
   	
   }
   //搜索线路
   public static function track_act_search($type=0,$city=NULL,$start=NULL,$end=NULL,
                                    $chuxing=0,$touristCount=-1,$age=NULL,
                                    $gender = NULL,$identity=NULL,$language = 0,
   		                    $price=0,$orders=0,$score=0,$limit=6,$offset=0)
   {
   //-----筛选出符合类型要求的活动和路线-----
   if(!empty($type)){
   	$tracks=DB::select('targetNid')->from('tiyu_categorygl')
   	          ->where('targetType','=','TRACK')
   	          ->where('categoryId','=',$type)
   	          ->execute()->as_array();
   	$acts=DB::select('targetNid')->from('tiyu_categorygl')
   	        ->where('targetType','=','ACTIVITY')
   		->where('categoryId','=',$type)
   		->execute()->as_array();
   	$trackIds=array();
   	$actIds=array();
   	if(!empty($tracks)){
        foreach ($tracks as $t){
   		$trackIds[]=$t['targetNid'];
   	}
        }
        if(!empty($acts)){
   	foreach ($acts as $a){
   		$actIds[]=$a['targetNid'];
   	}
        }
    }
     //-----------计算总数--------------------------------
    $count1 = DB::select('m.id')
   	         ->from(array('mod_track','m'))
   	         ->join(array('user_v2','u'))
   	         ->on('m.uid','=','u.uid')
   	         ->join(array('user_v2_userinfo','u2i'))
                 ->on('u.uid','=','u2i.uid')
                 ->where('m.status', '=', 'VALID');
   
    if(!empty($type)){
    	if(empty($trackIds)){
             $trackIds=array(0);
    	}
    	$count1=$count1->where('m.id','in',$trackIds);           
    }

    if(!empty($city)){
    	$count1=$count1->where('m.cityId','=',$city);
    }
    
    if(!empty($start)){
    	$id_start_m = self::get_line_with_start($start,'mod_track');
    	if(empty($id_start_m)){
    		$id_start_m = array(0);
    	}
    	$count1=$count1->where('m.id','in',$id_start_m);
    }
    
    if(!empty($end)){
    	$id_end_m = self::get_line_with_end($end,'mod_track');
    	if(empty($id_end_m)){
    		$id_end_m = array(0);
    	}
    	$count1=$count1->where('m.id','in',$id_end_m);
    }
    
    if($touristCount != -1 && $touristCount != ""){
    	$count1=$count1->where('m.serviceMaxPersons','>=',$touristCount);
    }
   	
    if(!empty($age) && $age != -1){
    	$count1=$count1->where('u2i.userAge','=',$age.'后');
    }
 
    if(!empty($gender)){
    	$count1=$count1->where('u.userGender','=',$gender);
    }
    
    if(!empty($identity)){
    	$count1=$count1->where('u.identityName','=',$identity);
    }
    
    if(!empty($language)){
    	if($language == 1){
    		$lkey = 'cn';
    	}else if($language == 2){
    		$lkey = 'fr';
    	}else if($language == 3){
    		$lkey = 'en';
    	}
    	$count1=$count1->where('u.'.$lkey,'=',1);
    }
    
    if(!empty($chuxing)){
    	if($chuxing==1){
    		$count1=$count1->where('m.trafficSupportCar','=',1);
    	}elseif($chuxing==2){
    		$count1=$count1->where('m.trafficSupportBus','=',1);
    	}
    }
    //---------------------------------------------------
    $count2 = DB::select('a.id')
                ->union($count1)
   	        ->from(array('mod_activity','a'))
   	        ->join(array('user_v2','u'))
   	        ->on('a.uid','=','u.uid')
   	        ->join(array('user_v2_userinfo','u2i'))
                ->on('u.uid','=','u2i.uid')
   	        ->where('a.status', '=', 'VALID');
   	
        if(!empty($type)){
             if(empty($actIds)){
    		$actIds=array(0);
    	     }
             $count2=$count2->where('a.id','in',$actIds);
        }
        
        if(!empty($city)){
   	       $count2=$count2->where('a.cityId','=',$city);
   	}
   	
   	if(!empty($start)){
   		$id_start_a = self::get_line_with_start($start,'mod_activity');
   		if(empty($id_start_a)){
   			$id_start_a = array(0);
   		}
   		$count2=$count2->where('a.id','in',$id_start_a);
   	}
   	
   	if(!empty($end)){
   		$id_end_a = self::get_line_with_end($end,'mod_activity');
   		if(empty($id_end_a)){
   			$id_end_a = array(0);
   		}
   		$count2=$count2->where('a.id','in',$id_end_a);
   	}
   	
   	if($touristCount != -1 && $touristCount != ""){
   		$count2=$count2->where('a.serviceMaxPersons','>=',$touristCount);
   	}
   	
        if(!empty($age) && $age != -1){
    	        $count2=$count2->where('u2i.userAge','=',$age.'后');
        }

   	if(!empty($gender)){
   		$count2=$count2->where('u.userGender','=',$gender);
   	}
   	
   	if(!empty($identity)){
   		$count2=$count2->where('u.identityName','=',$identity);
   	}
   	
   	if(!empty($language)){
   		$count2=$count2->where('u.'.$lkey,'=',1);
   	}
        if(!empty($chuxing)){
   		if($chuxing==1){
   			$count2=$count2->where('a.trafficSupportCar','=',1);
   		}elseif($chuxing==2){
   			$count2=$count2->where('a.trafficSupportBus','=',1);
   		}
   	}
  //--------------------------------------------------------------------------
     $query1 = DB::select('m.uid','m.id','m.city','m.name','m.thumb','m.summary','m.priceTotal',array('m.days','duration'),'m.score',
                'm.changed','m.type','m.zan','m.serviceMaxPersons','m.orders','m.trafficSupportCar','m.trafficSupportBus','u.userName','u2i.userIcon',
                'u.identityActive','u.telActive','u2i.userAge','u2i.userGender','u.identityName','u.cn','u.en','u.fr')
   	         ->from(array('mod_track','m'))
   	         ->join(array('user_v2','u'))
   	         ->on('m.uid','=','u.uid')
   	         ->join(array('user_v2_userinfo','u2i'))
                 ->on('u.uid','=','u2i.uid')
                 ->where('m.status', '=', 'VALID');
   
    if(!empty($type)){
        if(empty($trackIds)){
        	$trackIds=array(0);
        }
        $query1=$query1->where('m.id','in',$trackIds);        
    }
 
    if(!empty($city)){
    	$query1=$query1->where('m.cityId','=',$city);
    }
    
    if(!empty($start)){
    	$query1=$query1->where('m.id','in',$id_start_m);
    }
    
    if(!empty($end)){
    	$query1=$query1->where('m.id','in',$id_end_m);
    }
    
    if($touristCount != -1 && $touristCount != ""){
    	$query1=$query1->where('m.serviceMaxPersons','>=',$touristCount);
    }
   	
    if(!empty($age) && $age != -1){
    	$query1=$query1->where('u2i.userAge','=',$age.'后');
    }
 
    if(!empty($gender)){
    	$query1=$query1->where('u.userGender','=',$gender);
    }
    
    if(!empty($identity)){
    	$query1=$query1->where('u.identityName','=',$identity);
    }
    
    if(!empty($language)){
    	$query1=$query1->where('u.'.$lkey,'=',1);
    }
   
    if(!empty($chuxing)){
    	if($chuxing==1){
    		$query1=$query1->where('m.trafficSupportCar','=',1);
    	}elseif($chuxing==2){
    		$query1=$query1->where('m.trafficSupportBus','=',1);
    	}
    }
 
    if($price == 2 ){
    	$query1=$query1->order_by('priceTotal','asc');
    }else if($price == 1 ){
    	$query1=$query1->order_by('priceTotal','desc');
    }
    
    if($orders == 2 ){
    	$query1=$query1->order_by('orders','asc');
    }else if($orders == 1 ){
    	$query1=$query1->order_by('orders','desc');
    }
  
    if($score == 2 ){
    	$query1=$query1->order_by('score','asc');
    }else if($score == 1 ){
    	$query1=$query1->order_by('score','desc');
    }
    
   	if ($limit > 0)
   	{ $query1=$query1->offset($offset)->limit($limit);}
  //-------------------------------------------------------------------------- 	
    $query2 = DB::select('a.uid','a.id','a.city','a.name','a.thumb','a.summary','a.priceTotal',array('a.hours','duration'),'a.score',
                'a.changed','a.type','a.zan','a.serviceMaxPersons','a.orders','a.trafficSupportCar','a.trafficSupportBus','u.userName','u2i.userIcon',
                'u.identityActive','u.telActive','u2i.userAge','u2i.userGender','u.identityName','u.cn','u.en','u.fr')
            ->union($query1)
   	        ->from(array('mod_activity','a'))
   	        ->join(array('user_v2','u'))
   	        ->on('a.uid','=','u.uid')
   	        ->join(array('user_v2_userinfo','u2i'))
            ->on('u.uid','=','u2i.uid')
   	        ->where('a.status', '=', 'VALID');

        if(!empty($type)){
            if(empty($actIds)){
               $actIds=array(0);
            }
            $query2=$query2->where('a.id','in',$actIds);
        }

   	if(!empty($city)){
   	       $query2=$query2->where('a.cityId','=',$city);
   	}
   	
   	if(!empty($start)){
   		$query2=$query2->where('a.id','in',$id_start_a);
   	}
   	
   	if(!empty($end)){
   		$query2=$query2->where('a.id','in',$id_end_a);
   	}
   	
   	if($touristCount != -1 && $touristCount != ""){
   		$query2=$query2->where('a.serviceMaxPersons','>=',$touristCount);
   	}
   	
        if(!empty($age) && $age != -1){
    	        $query2=$query2->where('u2i.userAge','=',$age.'后');
        }

   	if(!empty($gender)){
   		$query2=$query2->where('u.userGender','=',$gender);
   	}
   	
   	if(!empty($identity)){
   		$query2=$query2->where('u.identityName','=',$identity);
   	}
   	
   	if(!empty($language)){
   		$query2=$query2->where('u.'.$lkey,'=',1);
   	}
        
        if(!empty($chuxing)){
   		if($chuxing==1){
   			$query2=$query2->where('a.trafficSupportCar','=',1);
   		}elseif($chuxing==2){
   			$query2=$query2->where('a.trafficSupportBus','=',1);
   		}
   	}
   	
   	$lines=$query2->execute()->as_array();  	
   	$counts = $count2->execute()->count();
   	
   	return array($counts,$lines);   	
   	
   }
   
   public static function getRebuildData($data)
   {
   	foreach($data as &$v){
   		$v['type'] = strtolower($v['type']);
   	}
   	return $data;
   }
   
   public static function get_line_with_start($date=NULL,$table)
   {
   	$data = DB::select('id','freetimeDays')
   	->from($table)
   	->where('status','=','VALID')
   	->execute()
   	->as_array();
   
   	$id = array();
   	$time = strtotime($date);
   	foreach($data as $v){
   		$days = explode(',',$v['freetimeDays']);
   		foreach($days as $day){
   			$day_time = strtotime($day);
   			if($time < $day_time||$time==$day_time){
   				$id[] = $v['id'];
   				break;
   			}
   		}
   	}
   
   	return $id;
   }
   
   public static function get_line_with_end($date=NULL,$table)
   {
   	$data = DB::select('id','freetimeDays')
   	->from($table)
   	->where('status','=','VALID')
   	->execute()
   	->as_array();
   
   	$id = array();
   	$time = strtotime($date);
   	foreach($data as $v){
   		$days = explode(',',$v['freetimeDays']);
   		foreach($days as $day){
   			$day_time = strtotime($day);
   			if($time > $day_time||$time==$day_time){
   				$id[] = $v['id'];
   				break;
   			}
   		}
   	}
   
   	return $id;
   }
   public function getLineTypeList()
   {
   	$types=DB::select('id','name')->from('tiyu_category')
   	       ->where('parentId','=',3)
   	       ->where('type','=','SUBCAT')
   	       ->execute()->as_array();
   	return $types;
   }
}
